﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml;
using DiscoveryLogic.Analytics.DataObjects.Rules;
using DiscoveryLogic.Common.Utilities;

namespace DiscoveryLogic.Analytics.DataObjects.Networks
{
    public class EntityRelationDefinition
    {
        #region props
        private string _SrcEntitySetName;
        /// <summary></summary>
        public string SrcEntitySetName
        {
            get
            {
                return this._SrcEntitySetName;
            }
            set
            {
                this._SrcEntitySetName = value;
            }
        }

        private string _TgtEntitySetName;
        /// <summary></summary>
        public string TgtEntitySetName
        {
            get
            {
                return this._TgtEntitySetName;
            }
            set
            {
                this._TgtEntitySetName = value;
            }
        }

        private LinkType _LinkType;
        /// <summary></summary>
        public LinkType LinkType
        {
            get
            {
                return this._LinkType;
            }
            set
            {
                this._LinkType = value;
            }
        }

        private string _LinkCalc;
        /// <summary></summary>
        public string LinkCalc
        {
            get
            {
                return this._LinkCalc;
            }
            set
            {
                this._LinkCalc = value;
            }
        }

        private List<ArgumentFieldBinding> _ArgBindings;
        /// <summary></summary>
        public List<ArgumentFieldBinding> ArgBindings
        {
            get
            {
                return this._ArgBindings;
            }
            set
            {
                this._ArgBindings = value;
            }
        }
        #endregion

        #region ctor
        public EntityRelationDefinition()
        {
            this._SrcEntitySetName = string.Empty;
            this._TgtEntitySetName = string.Empty;
            this._LinkType = LinkType.Citation;
            this._LinkCalc = string.Empty;
            this._ArgBindings=new List<ArgumentFieldBinding>();
        }

        public EntityRelationDefinition(string srcEntitySetName, 
            string tgtEntitySetName, LinkType linkType,
            string linkCalc, List<ArgumentFieldBinding> argBindings)
        {
            this._SrcEntitySetName = srcEntitySetName;
            this._TgtEntitySetName = tgtEntitySetName;
            this._LinkType = linkType;
            this._LinkCalc = linkCalc;
            this._ArgBindings = argBindings;
        }
        #endregion

        #region xml 
        public void Save(ref XmlDocument xDoc, XmlNode relationDefNode)
        {
            XmlDataUtil.UpdateAttribute(ref xDoc, relationDefNode,"SrcEntitySetName",this.SrcEntitySetName);
            XmlDataUtil.UpdateAttribute(ref xDoc, relationDefNode, "TgtEntitySetName", this.TgtEntitySetName);
            XmlDataUtil.UpdateAttribute(ref xDoc, relationDefNode, "LinkType", this.LinkType.ToString());
            XmlDataUtil.UpdateAttribute(ref xDoc, relationDefNode, "LinkCalc", this.LinkCalc);

            XmlNode argBindingNodes = XmlDataUtil.AddElement(ref xDoc, relationDefNode, "ArgBindings");
            if(this.ArgBindings !=null && this.ArgBindings.Count>0)
            {
                foreach(ArgumentFieldBinding argBinding in this.ArgBindings)
                {
                    XmlNode bindingNode = XmlDataUtil.AddElement(ref xDoc, argBindingNodes, "ArgBinding");
                    argBinding.Save(ref xDoc, bindingNode);
                }
            }
        }

        public static EntityRelationDefinition Read(XmlNode relationDefNode)
        {
            EntityRelationDefinition relDef=new EntityRelationDefinition();
            relDef.SrcEntitySetName = XmlDataUtil.GetAttributeValue(relationDefNode, "SrcEntitySetName", "");
            relDef.TgtEntitySetName = XmlDataUtil.GetAttributeValue(relationDefNode, "TgtEntitySetName", "");
            relDef.LinkType = (Networks.LinkType) Enum.Parse(typeof (LinkType),
                                                             XmlDataUtil.GetAttributeValue(relationDefNode, "LinkType",
                                                                                           Networks.LinkType.Citation.
                                                                                               ToString()));
            relDef.LinkCalc = XmlDataUtil.GetAttributeValue(relationDefNode, "LinkCalc", "");
            relDef.ArgBindings=new List<ArgumentFieldBinding>();
            XmlNodeList argBindingNodes = relationDefNode.SelectNodes("ArgBindings/ArgBinding");
            if(argBindingNodes !=null && argBindingNodes.Count>0)
            {
                foreach(XmlNode bindingNode in argBindingNodes)
                {
                    ArgumentFieldBinding argBinding = ArgumentFieldBinding.Read(bindingNode);
                    relDef.ArgBindings.Add(argBinding);
                }
            }
            return relDef;
        }
        #endregion
    }
}
